package ca.tatham.senssim;

import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

public class SensSimOptions extends Options
{
  private static final String ARG_LENGTH = "length";
  private static final String ARG_HELP = "help";
  private static final String ARG_OUTPUT = "output";
  private static final String ARG_GRIDSIZE = "gridsize";
  private static final String ARG_DENSITY = "density";
  private CommandLine m_cmd;

  private final int[] m_gridSizes =
  { 5 };
  private boolean m_ack;

  public SensSimOptions()
  {
    addOption("h", ARG_HELP, false, "Display this help");
    addOption("o", ARG_OUTPUT, true, "Output File");
    addOption("g", ARG_GRIDSIZE, true, "Grid Size");
    addOption("l", ARG_LENGTH, true, "Length of simulation, in ticks");
    addOption("d", ARG_DENSITY, true,
        "node density, percentage of nodes present (100 is full grid");
  }

  public void parse(String[] args) throws ParseException
  {
    CommandLineParser parser = new PosixParser();
    m_cmd = parser.parse(this, args);
  }

  public boolean isHelp()
  {
    return Boolean.getBoolean(m_cmd.getOptionValue(ARG_HELP));
  }

  public int[] getGridSizes()
  {
    if (m_cmd.hasOption(ARG_GRIDSIZE))
    {
      String[] split = m_cmd.getOptionValue(ARG_GRIDSIZE).split(",");
      int[] sizes = new int[split.length];
      int i = 0;
      for (String size : split)
      {
        sizes[i] = Integer.parseInt(size);
        i++;
      }
      return sizes;
    }
    return m_gridSizes;
  }

  public int getDensity()
  {
    if (m_cmd.hasOption(ARG_DENSITY))
    {
      return Integer.parseInt(m_cmd.getOptionValue(ARG_DENSITY));
    }
    return 100;
  }

  public int getDuration()
  {
    if (m_cmd.hasOption(ARG_LENGTH))
    {
      return Integer.parseInt(m_cmd.getOptionValue(ARG_LENGTH));
    }
    return 60 * 1000 * 5; // 5 minutes
  }

  public String getOutput()
  {
    if (m_cmd.hasOption(ARG_OUTPUT))
    {
      return m_cmd.getOptionValue(ARG_OUTPUT);
    }
    return "." + File.separatorChar + "simulation.txt";
  }

  public boolean isAck()
  {
    return m_ack;
  }

  public void setAck(boolean b)
  {
    m_ack = b;
  }

}
